//
// Created by PengYH on 2024/7/16.
//
#include <stdio.h>
#include <stdint.h>

#include <string>
#include <vector>

#include <gtest/gtest.h>
#include "ai_param_define.h"
#include "ai_struct_define.h"

/// CMDReq
TEST(CMDReq_TEST, CMD) {
  std::string cmd = R"({
    "module":"ALG_REQUEST_MESSAGE",
    "type":"get_alg_parking_identifier",
    "chnlid":0
  })";
  ai::CMDReq cmd_req;
  aigc::JsonHelper::JsonToObject(cmd_req, cmd);

  printf("module %s, type %s\n",
         cmd_req.module.c_str(), cmd_req.type.c_str());
  ASSERT_EQ(cmd_req.module, "ALG_REQUEST_MESSAGE");
  ASSERT_EQ(cmd_req.type, "get_alg_parking_identifier");
}

/// CMDResp
TEST(CMDResp_TEST, CMD) {
  ai::CMDResp cmd_resp;
  cmd_resp.type = "get_alg_parking_identifier";
  cmd_resp.state = 200;
  cmd_resp.err_msg = "All done";

  std::string cmd;
  bool bret = aigc::JsonHelper::ObjectToJson(cmd_resp, cmd);
  printf("resp %s\n", cmd.c_str());

  ASSERT_EQ(bret, true);
}

/// 获取停车场编号
TEST(CMDResp_GetAlgParkingIdentifier, CMD) {
  ai::CMDResp_GetAlgParkingIdentifier resp;
  resp.type = "get_alg_parking_identifier";
  resp.state = 200;
  resp.err_msg = "All done";
  resp.body.identifier = "MTEw";

  std::string cmd;
  bool bret = aigc::JsonHelper::ObjectToJson(resp, cmd);
  printf("resp %s\n", cmd.c_str());

  ASSERT_EQ(bret, true);
}

/// 设置停车场编号
TEST(CMDReq_SetAlgParkingIdentifier, CMD) {
  ai::CMDReq_SetAlgParkingIdentifier cmd;

  std::string str = R"({
    "module":"ALG_REQUEST_MESSAGE",
    "type":"set_alg_parking_identifier",
    "body": {
        "identifier":"MTEw"
    }
  })";
  aigc::JsonHelper::JsonToObject(cmd, str);

  printf("module %s, type %s\n",
         cmd.module.c_str(), cmd.type.c_str());
  ASSERT_EQ(cmd.body.identifier, "MTEw");
  ASSERT_EQ(cmd.module, "ALG_REQUEST_MESSAGE");
  ASSERT_EQ(cmd.type, "set_alg_parking_identifier");
}

/// 获取车位模式属性
TEST(CMDResp_GetAlgParkModeProp, CMD) {
  ai::CMDResp_GetAlgParkModeProp cmd;
  cmd.type = "get_alg_park_mode_prop";
  cmd.state = 200;
  cmd.err_msg = "All done";

  ai::CMDResp_GetAlgParkModeProp::Mode mode;
  mode.mode = 0;
  mode.content = "5bmz6KGM6L2m5L2N";
  cmd.body.modes.push_back(mode);

  mode.mode = 1;
  cmd.body.modes.push_back(mode);

  mode.mode = 2;
  cmd.body.modes.push_back(mode);

  std::string str;
  bool bret = aigc::JsonHelper::ObjectToJson(cmd, str);
  printf("cmd %s\n", str.c_str());

  ASSERT_EQ(bret, true);
}

/// 获取车位模式
TEST(CMDResp_GetAlgParkModePrm, CMD) {
  ai::CMDResp_GetAlgParkModePrm cmd;
  cmd.type = "get_alg_park_mode_prm";
  cmd.state = 200;
  cmd.err_msg = "All done";
  cmd.body.mode = 0;
  cmd.body.park_num = 3;

  std::string str;
  bool bret = aigc::JsonHelper::ObjectToJson(cmd, str);
  printf("cmd %s\n", str.c_str());

  ASSERT_EQ(bret, true);
}

/// 设置车位模式
TEST(CMDReq_SetAlgParkModePrm, CMD) {
  ai::CMDReq_SetAlgParkModePrm cmd;

  std::string str = R"({
    "module":"ALG_REQUEST_MESSAGE",
    "type":"set_alg_park_mode_prm",
    "body": {
        "mode": 0,
        "park_num": 3
    }
  })";
  aigc::JsonHelper::JsonToObject(cmd, str);

  printf("module %s, type %s\n",
         cmd.module.c_str(), cmd.type.c_str());
  ASSERT_EQ(cmd.body.mode, 0);
  ASSERT_EQ(cmd.body.park_num, 3);
  ASSERT_EQ(cmd.module, "ALG_REQUEST_MESSAGE");
  ASSERT_EQ(cmd.type, "set_alg_park_mode_prm");
}

/// 抓拍|实时结果
TEST(CMD_Result, CMD) {
//  std::string str = R"({"body":{"road_trigger_result":{"result":[{"alarm_info":{"alarm_error_type":0,"alarm_status":0},"alg_car_id":-1,"alg_report_mode":0,"can_drop":0,"car_series":"","car_stable_count":-1,"covered_event":0,"enter_start_timestamp":0,"force_leave_flag":0,"group_id":-1,"hk_plate":{"loc":{"bottom":0,"left":0,"right":0,"top":0},"plate_color":0,"plate_conf":0,"plate_str":"","plate_type":0},"mc_plate":{"loc":{"bottom":0,"left":0,"right":0,"top":0},"plate_color":0,"plate_conf":0,"plate_str":"","plate_type":0},"nonMotor_car_type":0,"nonmotor_id":-1,"out_video_start":0,"parking_state":8,"plate_color":0,"plate_loc":{"bottom":0,"left":0,"right":0,"top":0},"plate_mean":0.0,"plate_sharpness":0,"plate_str":"","plate_type":0,"reco_flag":1,"time_pts":8251941824,"time_s":1721294224998731,"zone_id":0,"zone_name":"WUItMDAx","zone_type":1}],"result_num":1,"trigger_type":7}},"chnlid":0,"type":"road_trigger_result"})";
//  std::string str = R"({"body":{"road_trigger_result":{"result":[{"alarm_info":{"alarm_error_type":0,"alarm_status":0},"alg_car_id":-1,"alg_report_mode":0,"can_drop":0,"car_series":"","car_stable_count":-1,"covered_event":0,"enter_start_timestamp":0,"force_leave_flag":0,"group_id":-1,"hk_plate":{"loc":{"bottom":0,"left":0,"right":0,"top":0},"plate_color":0,"plate_conf":0,"plate_str":"","plate_type":0},"mc_plate":{"loc":{"bottom":0,"left":0,"right":0,"top":0},"plate_color":0,"plate_conf":0,"plate_str":"","plate_type":0},"nonMotor_car_type":0,"nonmotor_id":-1,"out_video_start":0,"parking_state":8,"plate_color":0,"plate_loc":{"bottom":0,"left":0,"right":0,"top":0},"plate_mean":0.0,"plate_sharpness":0,"plate_str":"","plate_type":0,"reco_flag":1,"time_pts":3,"time_s":2,"zone_id":0,"zone_name":"WUItMDAx","zone_type":1}],"result_num":1,"trigger_type":7}},"chnlid":0,"type":"road_trigger_result"})";
  std::string str = R"({"body":{"road_trigger_result":{"result":[{"alarm_info":{"alarm_error_type":0,"alarm_status":0},"alg_car_id":1,"alg_report_mode":0,"can_drop":0,"car_ext_info":{"car_color_id":-1,"car_color_score":0,"car_color_str":"无","car_type_id":-1,"car_type_id_source":-1,"car_type_score":0,"car_type_str":"无","car_type_str_source":"无"},"car_loc":{"bottom":372,"left":1688,"right":1982,"top":40},"car_pos":2,"car_series":"","car_stable_count":11,"confidence":100,"covered_event":0,"enter_start_timestamp":0,"force_leave_flag":0,"group_id":-1,"has_car_body":true,"hk_plate":{"loc":{"bottom":0,"left":0,"right":0,"top":0},"plate_color":0,"plate_conf":0,"plate_str":"","plate_type":0},"mc_plate":{"loc":{"bottom":0,"left":0,"right":0,"top":0},"plate_color":0,"plate_conf":0,"plate_str":"","plate_type":0},"nonMotor_car_type":0,"nonmotor_id":-1,"out_video_start":1,"parking_state":1,"plate_color":1,"plate_covered":0,"plate_isocc":false,"plate_loc":{"bottom":285,"left":1821,"right":1896,"top":246},"plate_mean":106.415641784668,"plate_occ_rate":0,"plate_sharpness":99511,"plate_str":"粤S9Y508","plate_type":1,"recent_plate":false,"reco_flag":1,"roof_loc":{"bottom":120,"left":1733,"right":1961,"top":41},"sideWindow_loc":{"bottom":184,"left":1718,"right":1754,"top":68},"head_loc":{"bottom":332,"left":1742,"right":1980,"top":215},"time_pts":230000000,"time_s":1720750074,"window_loc":{"bottom":200,"left":1761,"right":1962,"top":115},"zone_id":1,"zone_name":"WUItMDAx2","zone_type":1,"trigger_extend":{"zone_id":0,"zone_type":0,"user_data":"some data","snap_mode":0}}],"result_num":1,"trigger_type":10}},"chnlid":0,"type":"road_trigger_result"})";

  ai::CMD_Result cmd;

//  std::string msg;
//  std::vector<std::string> keys;
  bool bret = aigc::JsonHelper::JsonToObject(cmd, str);

  printf("result num %d, type %d\n",
         cmd.body.road_trigger_result.result_num,
         cmd.body.road_trigger_result.trigger_type);
  ASSERT_EQ(bret, true);

  std::string sbody;
  aigc::JsonHelper::ObjectToJson(cmd.body, sbody);
  printf("body %s\n", sbody.c_str());
}

int main(int argc, char *argv[]) {
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}